package com.dxc.intern.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxc.intern.entity.Roster;
import com.dxc.intern.mapper.RosterMapper;
import com.dxc.intern.service.RosterService;
import com.dxc.untils.result.ResultUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 花名册(已入职) 前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@CrossOrigin
@RestController
@RequestMapping("/roster")
public class RosterController {

    @Autowired
    private RosterService rosterService;
    @Autowired
    private RosterMapper rosterMapper;

    // JSON返回
    // 对象 RESTful 风格返回
    @ApiOperation("测试接口返回")
    @RequestMapping(value = "hello",method = RequestMethod.GET)
    // 和上面相同 @GetMapping(value = "hello")
    public ResultUtil<String> hello(){
        return ResultUtil.success("你好");
    }

    @ApiOperation("花名册获取数据分页,demo")
    @RequestMapping(value = "listPage",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "自定义页码",name = "current"),
            @ApiImplicitParam(value = "自定义返回数量",name = "size"),
    })
    public ResultUtil<Page<Roster>> listPage(Integer current, Integer size){
        Page<Roster> page = new Page<>(current, size);
        rosterMapper.selectPage(page, Wrappers.query());
        return ResultUtil.success(page);
    }

    @ApiOperation("花名册更新数据")
    @RequestMapping(value = "updateRoster",method = RequestMethod.POST)
    // 和上面相同 @GetMapping(value = "hello")
    public ResultUtil<Roster> addRoster(@RequestBody Roster roster){
        rosterMapper.updateById(roster);
        //ResultUtil.success("插入成功", true);
        return ResultUtil.success(roster);
    }

    @ApiOperation("花名册删除数据")
    @RequestMapping(value = "deleteRoster",method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "删除ID",name = "id"),
    })
    public ResultUtil<Long> deleteRoster(Long id){
        rosterMapper.deleteById(id);
        //ResultUtil.success("插入成功", true);
        return ResultUtil.success(id);
    }
}
